package com.fin.zw.intelligentbot.controller.api;

import com.fin.zw.intelligentbot.annotation.CurrentUser;
import com.fin.zw.intelligentbot.entity.SceneInfo;
import com.fin.zw.intelligentbot.model.ResulteWraper;
import com.fin.zw.intelligentbot.model.base.Category;
import com.fin.zw.intelligentbot.model.base.Variable;
import com.fin.zw.intelligentbot.model.request.SceneInfoRequest;
import com.fin.zw.intelligentbot.model.response.SceneResponse;
import com.fin.zw.intelligentbot.model.session.SessionModel;
import com.fin.zw.intelligentbot.service.SceneInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/scene")
@Api(value = "对话场景", description = "对话场景")
public class SceneController {

    @Autowired
    private SceneInfoService sceneService;

    @GetMapping("/fetch")
    @ApiOperation(value = "查询对应的场景信息", notes = "查询对应的场景信息")
    public ResponseEntity<ResulteWraper> fetch(SceneInfoRequest condition, @ApiIgnore @CurrentUser SessionModel sessionModel)
    {
        ResulteWraper wraper = new ResulteWraper();
        List<SceneResponse> scenes = new ArrayList<>();
        List<SceneInfo> sceneInfos = this.sceneService.getSceneInfoList(sessionModel);
        sceneInfos.forEach(s->{
            SceneResponse scene = new SceneResponse();
            BeanUtils.copyProperties(s,scene);
            if (s.getVariables() != null && !s.getVariables().isEmpty()) {
                Set<Variable> variables = s.getVariables().stream().map(m -> {
                    Variable variable = new Variable();
                    BeanUtils.copyProperties(m, variable);
                    return variable;
                }).collect(Collectors.toSet());
                scene.setVariables(variables);
            }
            if (s.getCategories() != null && !s.getVariables().isEmpty()) {
                Set<Category> categories = s.getCategories().stream().map(categoryLabel -> {
                    Category category = new Category();
                    BeanUtils.copyProperties(categoryLabel, category);
                    return category;
                }).collect(Collectors.toSet());
                scene.setCategories(categories);
            }
            scenes.add(scene);
        });
        wraper.setData(scenes);
        return ResponseEntity.ok().body(wraper);
    }
}

